<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>角色资源管理页面</title>
<link rel="stylesheet" href="<c:url value='/admin/js/util/ztree/css/demo.css'/>" type="text/css"></link>
<link rel="stylesheet" href="<c:url value='/admin/js/util/ztree/css/zTreeStyle/zTreeStyle.css'/>" type="text/css"></link>
<script type="text/javascript" src="<c:url value='/admin/js/util/ztree/js/jquery-1.4.4.min.js'/>"></script>
<script type="text/javascript" src="<c:url value='/admin/js/util/ztree/js/jquery.ztree.core-3.4.js'/>"></script>
<script type="text/javascript" src="<c:url value='/admin/js/util/ztree/js/jquery.ztree.excheck-3.4.js'/>"></script>
<script type="text/javascript">
function submitForm() {
	getValue();
	$("#mainForm").attr("action", "<c:url value='/admin/roleAction!treeMenuExc.do' />");
	$("#mainForm").submit();
}
</script>
</head>
<body>
	<form id="mainForm" action="" method="post">
	<input type="hidden" name="roleId" id="roleId" value="${ requestScope.roleId }" />
	<input type="hidden" name="checkedIds" id="checkedIds" value="" />	
	
	<div class="content_wrap">
		<div class="zTreeDemoBackground left">
			<ul id="menuTree" class="ztree"></ul>
		</div>
		<div class="right">
			<table width="100%" cellspacing="1">
				<tr>
					<td><a href="javascript:submitForm();">保存</a> <a href="javascript:history.go(-1);">返回</a></td>
				</tr>
			</table>
		</div>
	</div>
	</form>
	<script language="javascript">	
		var setting = {
			check: {
				enable: true,
				chkboxType: { "Y" : "ps", "N" : "ps" }
			},
			data: {
				simpleData: {
					enable: true
				}
			}
		};

		var zNodes;
		var myMenuIds = new Array();
		var selectMenuIds = new Array();
		
		$.ajax({
			type:"POST",
			url:"<c:url value='/admin/menuAction!getAllMenu.do'/>",
			async:false,				
			data:{},
			dataType:"json",
			success:function(data){				
				zNodes = data;
				
				$.ajax({
					type:"POST",
					url:"<c:url value='/admin/roleAction!getAllSelectedMenu.do'/>",
					async:false,				
					data:{ "roleId" : '${ requestScope.roleId }'},
					dataType:"json",
					success:function(data){
						$(data).each(function(i, item) {
							myMenuIds[i] = item.id;
						});
					}
				});
			}
		});
		
		function getValue() {
			var treeObj = $.fn.zTree.getZTreeObj("menuTree");
			var nodes = treeObj.getCheckedNodes(true);
			var j = 0;
			for (var i = 0; i < nodes.length; i++) {
				//返回时，拼装的菜单部分，不做为返回，否则会产生脏数据
				if (nodes[i].pId != null) {
					selectMenuIds[j ++] = nodes[i].id;
				}				
			}
			$("#checkedIds").val(selectMenuIds.toString());
		}
		
		function checkedNode(id) {
			var treeObj = $.fn.zTree.getZTreeObj("menuTree");
			var node = treeObj.getNodeByParam("id", id, null);
			node.checked = true;
			treeObj.updateNode(node);
			
			//把最顶级的根节点（拼装的菜单部分）级联选中
			if (node.level == 1) {
				var pNode = node.getParentNode();
				pNode.checked = true;
				treeObj.updateNode(pNode);
			}			
		}
		
		$(document).ready(function(){
			$.fn.zTree.init($("#menuTree"), setting, zNodes);
			
			if (myMenuIds.length > 0) {
				for (var i = 0; i < myMenuIds.length; i ++) {
					checkedNode(myMenuIds[i]);
				}
			}
		});	
	</script>
</body>
</html>
